var getStrCharLen = (val) => {
	var str = new String(val);
	var bytesCount = 0;
	for (var i = 0, n = str.length; i < n; i++) {
		var c = str.charCodeAt(i);
		if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
			bytesCount += 1;
		} else {
			bytesCount += 2;
		}
	}
	return bytesCount;
}

var repeat = (str, n) => {
	return new Array(n + 1).join(str);
}

var escape2Html = (str) => {
	if (!str) return '';
	return str.replace(/<image/gi, '<image style="max-width: 100%;height: auto;" ')
		.replace(/<section/g, '<div')
		.replace(/<article/g, '<div')
		.replace(/\/section>/g, '/div>')
		.replace(/\/article>/g, '/div>')
}

var formatTime = (number, format = 'Y年M月D日 h:m:s') => {
	var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
	var returnArr = [];

	var date = new Date(number * 1000);
	returnArr.push(date.getFullYear());
	returnArr.push(formatNumber(date.getMonth() + 1));
	returnArr.push(formatNumber(date.getDate()));

	returnArr.push(formatNumber(date.getHours()));
	returnArr.push(formatNumber(date.getMinutes()));
	returnArr.push(formatNumber(date.getSeconds()));

	for (var i in returnArr) {
		format = format.replace(formateArr[i], returnArr[i]);
	}
	return format;
}
var monthDate = () => {
	var date = new Date();
	return date.getMonth() + 1 + '月' + date.getDate() + '日'
}
var week = () => {
	var a = new Date().getDay();
	var b = "";
	switch (a) {
		case 0:
			b = "Sunday";
			break;
		case 1:
			b = "Monday";
			break;
		case 2:
			b = "Tuesday";
			break;
		case 3:
			b = "Wednesday";
			break;
		case 4:
			b = "Thursday";
			break;
		case 5:
			b = "Friday";
			break;
		default:
			b = "Saturday";
	}
	return b;
}
var formatNumber = (n) => {
	n = n.toString()
	return n[1] ? n : '0' + n
}
//获取当前日期函数
function getNowFormatDate() {
	let date = new Date(),
		year = date.getFullYear(), //获取完整的年份(4位)
		month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
		strDate = date.getDate() // 获取当前日(1-31)
	if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0
	if (strDate < 10) strDate = `0${strDate}` // 如果日是个位数，在前面补0

	return `${year}-${month}-${strDate}`
}
//获取年月函数
function getNowYearMonth() {
	let date = new Date(),
		year = date.getFullYear(), //获取完整的年份(4位)
		month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月)
		strDate = date.getDate() // 获取当前日(1-31)
	if (month < 10) month = `0${month}` // 如果月份是个位数，在前面补0

	return `${year}-${month}`
}
// 解析URL 参数
function changeChinese(val) {
	var str = null
	if (val == 1) {
		str = '一'
	} else if (val == 2) {
		str = '二'
	} else if (val == 3) {
		str = '三'
	} else if (val == 4) {
		str = '四'
	} else if (val == 5) {
		str = '五'
	}
	return str

}
/* 
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性：max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉<br/>标签
 * @param html
 * @returns {void|string|*}
 */
function formatRichText(html) {
	let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
		return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	newContent = newContent.replace(/\<img/gi,
		'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
	return newContent;
}
export default {
	getStrCharLen,
	repeat,
	escape2Html,
	formatTime,
	changeChinese,
	formatRichText,
	monthDate,
	week,
	getNowFormatDate,
	getNowYearMonth
}